<template>
  <div >
    <!--  面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>消息提醒</el-breadcrumb-item>
      <el-breadcrumb-item>用户反馈</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  卡片视图区域-->
    <el-card>
      <!-- 用户列表区域 -->
      <el-table :data="messagelist" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="用户名" prop="userName" width="150px"></el-table-column>
        <el-table-column label="角色" prop="userPower">
          <template slot-scope="scope">
            <span v-if="scope.row.userPower == 2">普通用户</span>
            <span v-if="scope.row.userPower == 1">管理员</span>
          </template>
        </el-table-column>
        <el-table-column label="企业Id" prop="companyId"></el-table-column>
        <el-table-column label="反馈消息" prop="userComment" width="500px"></el-table-column>
        <el-table-column label="反馈时间" prop="submitTime"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-button type="primary" icon="el-icon-edit" size="mini"
                       @click="showEditDialog(scope.row)"></el-button>
            <!--删除按钮-->
            <el-button type="danger" icon="el-icon-delete" size="mini"
                       @click="removeUserById(scope.row.userCommentId)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--    分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageIndex"
        :page-sizes="[10, 50, 100]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total">
      </el-pagination>
    </el-card>
    <!--  修改用户的对话框-->
    <el-dialog
      title="修改反馈消息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogVisibleClosed">
      <!--    内容主体区域-->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <!-- 用户名 -->
        <el-form-item label="用户名">
          <el-input v-model="editForm.userName" prefix-icon="el-icon-user-solid" disabled></el-input>
        </el-form-item>
        <!--        &lt;!&ndash; 邮箱 &ndash;&gt;-->
        <!--        <el-form-item label="邮箱" prop="mail">-->
        <!--          <el-input v-model="editForm.mail" prefix-icon="el-icon-message"  clearable></el-input>-->
        <!--        </el-form-item>-->
        <el-form-item label="角色">
          <el-select v-model="editForm.userPower" disabled>
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!--  反馈消息    -->
        <el-form-item label="消息" prop="message">
          <el-input v-model="editForm.userComment" prefix-icon="el-icon-chat-line-round" clearable></el-input>
        </el-form-item>
        <!--  反馈消息    -->
        <el-form-item label="反馈时间" prop="time">
          <el-input v-model="editForm.submitTime" prefix-icon="el-icon-watch" disabled></el-input>
        </el-form-item>
      </el-form>
      <!--    底部区域-->
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editMessageInfo">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // select选项
      options: [{
        value: '1',
        label: '管理员'
      },  {
        value: '2',
        label: '普通用户'
      }],
      // 获取用户列表的参数对象
      queryInfo: {
        // 当前的页数
        pageIndex: 1,
        // 当前每页显示多少条数据
        pageSize: 10
      },
      messagelist: [
      ],
      total: 0,
      // 控制添加用户框的显示与隐藏
      addDialogVisible: false,
      // 控制修改用户框的显示与隐藏
      editDialogVisible: false,
      // 修改用户信息表单
      editForm: {},
      // 修改用户信息表单的验证规则对象
      editFormRules: {
        userComment: [
          { required: true, message: '请输入用户的反馈消息', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getMessageList()
  },
  methods: {
    getMessageList () {
      var that = this
      that.$http.post('comment/page', that.queryInfo).then(function (res) {
        if (res.data.code == 200) {
          that.messagelist = res.data.data
          that.total = res.data.count
        } else {
          return that.$message.error('获取失败！')
        }
      })
    },
    // 监听pagesize改变的事件
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.getMessageList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.pageIndex = newPage
      this.getMessageList()
    },
    // 监听 switch 开关状态的改变
    async isFixChanged (userinfo) {
      console.log(userinfo)
      const { data: res } = await this.$http.put(`${userinfo.id}/${userinfo.isFro}`)
      if (res.state !== 200) {
        userinfo.isFix = !userinfo.isFix
        return this.$message.error('消息状态更新失败！')
      }
      this.$message.success('更新消息状态成功！')
    },
    // 监听关闭修改用户对话框的的关闭事件
    editDialogVisibleClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 展示编辑用户的对话框
    showEditDialog (row) {
      // console.log(row)
      this.editDialogVisible = true
      this.editForm = JSON.parse(JSON.stringify(row))
      // console.log(this.editForm)
    },
    // 修改信息并提交
    editMessageInfo () {
      this.editDialogVisible = false
      this.$refs.editFormRef.validate(async vaild => {
        if (!vaild) {}
        var that = this
        that.$http.post('comment/update', that.editForm).then(function (res) {
          if (res.data.code == 200) {
            that.getMessageList();
            return that.$message.success('编程成功！')
          } else {
            return that.$message.error('获取失败！')
          }
        })
      })
    },
    // 更加Id删除对应的用户信息
    async removeUserById (id) {
      // console.log(id)
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm('此操作将永久删除该消息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      // console.log('确认了删除')
      var that = this
      that.$http.post('comment/delete', {userCommentId:id}).then(function (res) {
        if (res.data.code == 200) {
          that.getMessageList();
          return that.$message.success('删除成功！')
        } else {
          return that.$message.error('删除失败！')
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
